{% import 'macros/form.html' as form %}

{% block resource_view %}
  <div id="view-{{ resource_view['id'] }}" class="resource-view" data-id="{{ resource_view['id'] }}" data-title="{{ resource_view['title'] }}" data-description="{{ resource_view['descripion'] }}">
  <div class="actions">
    <a class="btn btn-default"
       target="_blank"
       href="{{ h.url_for('resource_view', id=package['name'], resource_id=resource['id'], view_id=resource_view['id'], qualified=True) }}">
      <i class="fa fa-arrows-alt"></i>
      {{ _("Fullscreen") }}
    </a>
    <a class="btn btn-default"
       href="#embed-{{ resource_view['id'] }}"
       data-module="resource-view-embed"
       data-module-id="{{ resource_view['id'] }}"
       data-module-url="{{ h.url_for('resource_view', id=package['name'], resource_id=resource['id'], view_id=resource_view['id'], qualified=True) }}">
      <i class="fa fa-code"></i>
      {{ _("Embed") }}
    </a>
  </div>
  <p class="desc">{{ h.render_markdown(resource_view['description']) }}</p>
    <div class="m-top ckanext-datapreview">
      {% if not to_preview and h.resource_view_is_filterable(resource_view) %}
        {% snippet 'package/snippets/resource_view_filters.html', resource=resource %}
      {% endif %}
      {% if not h.resource_view_is_iframed(resource_view) %}
        {{ h.rendered_resource_view(resource_view, resource, package) }}
      {% else %}
        <div class="data-viewer-error js-hide">
          <p class="text-danger">
            <i class="fa fa-info-circle"></i>
            {{ _('This resource view is not available at the moment.') }}
            <a href="#" data-toggle="collapse" data-target="#data-view-error">
              {{ _('Click here for more information.') }}
            </a>
          </p>
          <p id="data-view-error" class="collapse"></p>
          <p>
            <a href="{{ resource.url }}" class="btn btn-default btn-lg resource-url-analytics" target="_blank">
              <i class="fa fa-lg fa-arrow-circle-o-down"></i>
              {{ _('Download resource') }}
            </a>
          </p>
        </div>
        {% if not to_preview %}
          {% set current_filters = request.args.get('filters') %}
          {% if current_filters %}
            {% set src = h.url_for(package['type'] ~ '_resource.view', id=package['name'],
              resource_id=resource['id'],
              view_id=resource_view['id'],
              filters=current_filters, qualified=true)  %}
          {% else %}
            {% set src = h.url_for(package['type'] ~ '_resource.view', id=package['name'],
              resource_id=resource['id'],
              view_id=resource_view['id'], qualified=true)  %}
          {% endif %}
        {% else %}
          {# When previewing we need to stick the whole resource_view as a param as there is no other way to pass to information on to the iframe #}
          {% set src = h.url_for(package['type'] ~ '_resource.view', id=package['name'], resource_id=resource['id'], qualified=true) + '?' + h.urlencode({'resource_view': h.dump_json(resource_view)}) %}
        {% endif %}
        <iframe src="{{ src }}" frameborder="0" width="100%" data-module="data-viewer">
          <p>{{ _('Your browser does not support iframes.') }}</p>
        </iframe>
      {% endif %}
    </div>
    <div id="embed-{{ resource_view['id'] }}" class="modal fade resource-view-embed">
      <div class="modal-dialog">
    <div class="modal-content">

      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h3 class="modal-title">{{ _("Embed resource view") }}</h3>
      </div>
      <div class="modal-body">
        <p class="embed-content">{{ _("You can copy and paste the embed code into a CMS or blog software that supports raw HTML") }}</p>
        <div class="row">
          <div class="col-md-6">
            {{ form.input("width", label=_("Width"), value=700, classes=["control-full"]) }}
          </div>
          <div class="col-md-6">
            {{ form.input("height", label=_("Height"), value=400, classes=["control-full"]) }}
          </div>
        </div>
        {{ form.textarea("code", label=_("Code"), value="", classes=["pre"], rows=3) }}
      </div>
      </div>
      </div>
    </div>
  </div>
{% endblock %}
